<template>
  <ul class="todo-main">
    <Item v-for="todo in todoList" :key="todo.id" v-bind="todo" />
  </ul>
</template>

<script>
import { v4 as uuidv4 } from "uuid";
import Item from "./Item";
export default {
  name: "List",
  data() {
    return {
      todoList: [
        { id: uuidv4(), thing: "唱", done: false },
        { id: uuidv4(), thing: "跳", done: true },
        { id: uuidv4(), thing: "rap", done: false },
      ],
    };
  },
  components: {
    Item,
  },
  mounted(){
    // 给$bus绑定一个接收header组件传值的事件newTodo
    this.$bus.$on("newTodo",(newTodo)=>{
      this.todoList.unshift(newTodo)
    })

    // 调用$bus上的getTodo事件,给footer传todoList
    this.$bus.$emit("getTodo",this.todoList)
  },
  watch : {
    todoList : {
      deep : true,
      handler(){
        this.$bus.$emit("getTodo",this.todoList)
      }
    }
  }
};
</script>

<style scoped>
/*main*/
.todo-main {
  margin-left: 0px;
  border: 1px solid #ddd;
  border-radius: 2px;
  padding: 0px;
}

.todo-empty {
  height: 40px;
  line-height: 40px;
  border: 1px solid #ddd;
  border-radius: 2px;
  padding-left: 5px;
  margin-top: 10px;
}
</style>